package net.anzhuo.controller.db.report;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


import lombok.extern.slf4j.Slf4j;
import net.anzhuo.common.entity.PaginationVO;
import net.anzhuo.common.utils.RedisUtil;
import net.anzhuo.entity.GsBidding;

import net.anzhuo.entity.db.Bidding.ResponseResult;
import net.anzhuo.entity.db.Bidding.Result;
import net.anzhuo.service.GsBiddingService;

/**
 * 
 * @author jiyk
 *   招投标
 */
@RestController
@Slf4j
public class BiddingController {

	@Autowired
	private GsBiddingService gsBiddingService;

	
	@RequestMapping("/Bidding/BiddingInfo")
	public ResponseResult BiddingInfo(String type,String searchKey,String pageIndex, String pageSize) {
		log.info("===>BiddingInfo_searchKey="+searchKey+", pageSize="+pageSize);
		long startTime=System.currentTimeMillis();
		if (StringUtils.isBlank(pageSize)) {
			pageSize = "50";
		}
		
		ResponseResult rr = null;
		try {
			rr = (ResponseResult)RedisUtil.get("BiddingInfo:"+searchKey);
			if (rr != null) {
				 long endTime=System.currentTimeMillis();
				 float excTime=(float)(endTime-startTime)/1000;
				 log.info(" BiddingInfo 执行时间："+excTime+"s");
				return rr;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		GsBidding gsBidding = new GsBidding();
	//	GsBase base = gsBaseService.getByName(searchKey);
		gsBidding.setCoMd5(searchKey);
		gsBidding.setPageSize(Integer.parseInt(pageSize));
		PaginationVO<GsBidding> gsBiddingList = gsBiddingService.getListWithPage(gsBidding);
		System.out.println("keyno="+searchKey+",gsBiddingList="+gsBiddingList.getList().size());
		log.info("keyno="+searchKey+",gsBiddingList="+gsBiddingList.getList().size());
		ResponseResult responseResult = new ResponseResult();
		responseResult.setStatus("200");
		responseResult.setMessage("查询成功");
		
		net.anzhuo.entity.db.Bidding.Paging paging = new net.anzhuo.entity.db.Bidding.Paging();
		paging.setPageIndex(1);
		paging.setPageSize(Integer.parseInt(pageSize));
		paging.setTotalRecords(gsBiddingList.getTotalCount());
		responseResult.setPaging(paging);
		
		List<Result> resultList = new ArrayList<Result>();
		Result result = null;
		if (gsBiddingList.getList()!=null && gsBiddingList.getList().size()>0) {
			for (GsBidding g : gsBiddingList.getList()) {
				result = new Result();
		        result.setPublishTime(g.getPublishTime());
		        result.setTitle(g.getTitle());
		        result.setPurchaser(g.getPurchaser());
				resultList.add(result);
			}
		}
		responseResult.setResult(resultList);
		
		try {
			RedisUtil.set("BiddingInfo:"+searchKey, responseResult);
		} catch (Exception e) {
			e.printStackTrace();
		}
		 long endTime=System.currentTimeMillis();
		 float excTime=(float)(endTime-startTime)/1000;
		 log.info(" BiddingInfo 执行时间："+excTime+"s");
		return responseResult;
	}
	
	
	
	
	
}
